<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Data_kelas extends CI_Controller
{
    public function index()
    {
        $data['rslt_data_kelas'] = $this->db->query('SELECT k.id_kelas, t.tahun_angkatan, k.nama_kelas, ks.tingkat_kelas, ks.status_kelas, COUNT(s.id_siskel) AS jumlah_siswa, k.keterangan FROM data_kelas k
		LEFT JOIN tahun t ON(t.id_tahun=k.id_tahun)
		LEFT JOIN kelas_stat ks ON(ks.id_kelstat=k.id_kelstat)
		LEFT JOIN siswa_kelas s ON(s.id_kelas=k.id_kelas)
		GROUP BY k.id_kelas
		ORDER BY t.tahun_angkatan DESC, ks.tingkat_kelas ASC, k.nama_kelas ASC');
        $this->template->admin('admin/v_data_kelas', $data, 2, "Data Kelas");
    }
	
	public function tambah_kelas()
	{
		$this->load->library('form_validation');
		$this->_rule_validation_kelas();
		if ($this->form_validation->run() === FALSE)
		{
			$data['select_tahun'] = $this->db->from('tahun')->order_by('id_tahun', 'ASC')->get();
			$data['select_kelas'] = $this->db->from('kelas_stat')->order_by('id_kelstat', 'ASC')->get();
			$data['id'] = '';
			$data['id_tahun'] = $this->db->select('id_tahun')->from('tahun')->where('tahun_aktif', '1')->get()->row()->id_tahun;
			$data['id_kelstat'] = '';
			$data['nama_kelas'] = '';
			$data['keterangan'] = '';
			$this->template->admin('admin/v_data_kelas_formkelas', $data, 2, "Tambah Data Kelas");
		}
		else
		{
			$this->_simpan('TAMBAH_KELAS');
		}
	}
    
    public function edit_kelas($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $rslt_data = $this->db->select('*')->from('data_kelas')->where('id_kelas', $id)->where('id_tahun !=', 'null')->limit(1)->order_by('id_kelas', 'ASC')->get();
            if($rslt_data->num_rows() == 1)
            {
				$this->load->library('form_validation');
				$this->_rule_validation_kelas();
				if ($this->form_validation->run() === FALSE)
				{
					$rslt_data = $rslt_data->row();
					$data['select_tahun'] = $this->db->from('tahun')->order_by('id_tahun', 'DESC')->get();
					$data['select_kelas'] = $this->db->from('kelas_stat')->order_by('id_kelstat', 'ASC')->get();
					$data['id'] = $rslt_data->id_kelas;
					$data['id_tahun'] = $rslt_data->id_tahun;
					$data['id_kelstat'] = $rslt_data->id_kelstat;
					$data['nama_kelas'] = $rslt_data->nama_kelas;
					$data['keterangan'] = $rslt_data->keterangan;
					$this->template->admin('admin/v_data_kelas_formkelas', $data, 2, "Ubah Data Kelas");
				}
				else
				{
					$this->_simpan('UBAH_KELAS');
				}
            }
            else
            {
                show_404();
            }
        }
    }
    
	public function tambah_ruangan()
	{
		$this->load->library('form_validation');
		$this->_rule_validation_ruangan();
		if ($this->form_validation->run() === FALSE)
		{
			$data['id'] = '';
			$data['nama_kelas'] = '';
			$data['keterangan'] = '';
			$this->template->admin('admin/v_data_kelas_formruangan', $data, 2, "Tambah Data Ruangan");
		}
		else
		{
			$this->_simpan('TAMBAH_RUANGAN');
		}
	}
    
    public function edit_ruangan($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $rslt_data = $this->db->select('*')->from('data_kelas')->where('id_kelas', $id)->where('id_tahun', NULL)->limit(1)->order_by('id_kelas', 'ASC')->get();
            if($rslt_data->num_rows() == 1)
            {
				$this->load->library('form_validation');
				$this->_rule_validation_ruangan();
				if ($this->form_validation->run() === FALSE)
				{
					$rslt_data = $rslt_data->row();
					$data['id'] = $rslt_data->id_kelas;
					$data['nama_kelas'] = $rslt_data->nama_kelas;
					$data['keterangan'] = $rslt_data->keterangan;
							
					$this->template->admin('admin/v_data_kelas_formruangan', $data, 2, "Ubah Data Ruangan");
				}
				else
				{
					$this->_simpan('UBAH_RUANGAN');
				}
            }
            else
            {
                show_404();
            }
        }
    }
    
    public function hapus($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $this->db->where('id_kelas', $id)->limit(1)->delete('data_kelas');
            redirect('admin/data_kelas');
        }
    }
    
    public function _simpan($proses = null)
    {
        if($proses == null)
        {
            show_404();
        }
        else
        {
            $id_tahun = $this->input->post('id_tahun');
            $id_kelstat = $this->input->post('id_kelstat');
            $tingkat = $this->input->post('tingkat_kelas');
            $nama = $this->input->post('nama_kelas');
            $keterangan = $this->input->post('keterangan');
			$data_simpan_kelas = array('id_tahun' => $id_tahun,
                                'id_kelstat' => $id_kelstat,
                                'nama_kelas' => $nama,
                                'keterangan' => $keterangan);
			$data_simpan_ruangan = array('nama_kelas' => $nama,
                                'keterangan' => $keterangan);
            if($proses == 'TAMBAH_KELAS')
            {
                $this->db->insert('data_kelas', $data_simpan_kelas);
            }
            else if($proses == 'UBAH_KELAS')
            {
                $id = $this->input->post('id');
                if($id != '')
                {
                    $this->db->set($data_simpan_kelas)
                        ->where('id_kelas', $id)
                        ->update('data_kelas');
                }
            }
			elseif($proses == 'TAMBAH_RUANGAN')
            {
                $this->db->insert('data_kelas', $data_simpan_ruangan);
            }
            else if($proses == 'UBAH_RUANGAN')
            {
                $id = $this->input->post('id');
                if($id != '')
                {
                    $this->db->set($data_simpan_ruangan)
                        ->where('id_kelas', $id)
                        ->update('data_kelas');
                }
            }
            redirect('admin/data_kelas');
        }
    }
	
	private function _rule_validation_kelas()
	{
		$this->form_validation->set_message('required', '%s harap di isi');
		$this->form_validation->set_rules('id_tahun', 'Tahun', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('id_kelstat', 'Status Kelas', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('nama_kelas', 'Nama Kelas', 'required|max_length[20]|trim');
		$this->form_validation->set_rules('keterangan', 'Keterangna', 'max_length[50]|trim');
	}
	private function _rule_validation_ruangan()
	{
		$this->form_validation->set_message('required', '%s harap di isi');
		$this->form_validation->set_rules('nama_kelas', 'Nama Kelas', 'required|max_length[20]|trim');
		$this->form_validation->set_rules('keterangan', 'Keterangna', 'max_length[50]|trim');
	}
}

/* End of file data_kelas.php */
/* Location: ./application/controllers/admin/data_kelas.php */